package customer.zspt.cn.customer.components.order.orderVp;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import customer.zspt.cn.customer.Adapter.OrderMsgAdapter;
import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.bean.OrderMsgDao;
import customer.zspt.cn.customer.bean.OrderReplyDao;
import customer.zspt.cn.customer.bean.OrderStateDao;
import customer.zspt.cn.customer.utils.GetAllInfoUtils;
import customer.zspt.cn.customer.utils.SPUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/7/18 0018.
 */

public class OrderSecondVpItem extends BaseVpItem implements View.OnClickListener{

    private View view;
    private OrderMsgAdapter adapter;
    //vp第二個界面
    @ViewInject(R.id.chat_ll)
    private LinearLayout chat_ll;
    @ViewInject(R.id.sc_ll)
    private LinearLayout sc_ll;

    @ViewInject(R.id.replyMsg)
    private EditText replyMsg;

    @ViewInject(R.id.send)
    private Button send;

    private BitmapUtils utils;
    private List<OrderStateDao.OrderState> msg_ls;
    private int orderId;
    private Handler mHandler= new Handler(){
        @Override
        public void handleMessage(Message msg) {
            addMsgView();
        }
    };
    public OrderSecondVpItem(Activity activity, OrderMsgDao.OrderMsg orderMsg, String guserid, int orderId) {
        super(activity, orderMsg,guserid);
        this.orderId = orderId;
    }
    @Override
    public View initView() {
        view = View.inflate(mActivity, R.layout.order_view_vp_sc,null);
        ViewUtils.inject(this,view);
       utils  = new BitmapUtils(mActivity);
        return view;
    }
    @Override
    public void initData() {
        StateView();
    }
    private void StateView(){
            chat_ll.removeAllViews();
            String token = SPUtils.getString(mActivity,"token","");
            GetAllInfoUtils.infoUtils.getFriendInfo(constant.AllMSG + "/orderReply/list?orderId="+mOrderMsg.orderId, token, mActivity);
            GetAllInfoUtils.infoUtils.monitoringLinster(new GetAllInfoUtils.ReadyData() {
                @Override
                public void success(String data) {
                    pushData(data);
                }
                @Override
                public void fail() {
                }
            });
        send.setOnClickListener(this);
    }
    //解析回復信息
    private void pushData(String data) {
        try{
            Gson gson = new Gson();
            OrderStateDao orderStateDao = gson.fromJson(data, OrderStateDao.class);
            msg_ls = orderStateDao.data;
            Log.e("size",msg_ls.size()+"");
            if(msg_ls.size()>0)
            {
                mHandler.sendEmptyMessage(0);
            }
            else{
               if(orderId== mGuserid){
                   sc_ll.setVisibility(View.GONE);
               }
               else{
                   replyMsg.setHint("追加信息");
               }
            }

        }catch (Exception e)
        {
           e.printStackTrace();
        }
    }
    //添加对话数据
    private void addMsgView() {

        for(int position=0;position<msg_ls.size();position++)
        {
            View view = View.inflate(mActivity, R.layout.order_sc_msg_item,null);
            ImageView user_header_ig = view.findViewById(R.id.user_header_ig);
            TextView userName = view.findViewById(R.id.userName);
            TextView msg = view.findViewById(R.id.msg);
            utils.display(user_header_ig,msg_ls.get(position).kefu.kefuAvatar);
            userName.setText(msg_ls.get(position).kefu.kefuNickname);
            msg.setText(msg_ls.get(position).orderReplyContent);
            chat_ll.addView(view);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.send:
                String content  =  replyMsg.getText().toString();
                Packaging(content);
                break;
        }
    }
    private void Packaging(String content) {
        OrderReplyDao replyDao = new OrderReplyDao();
        replyDao.userid  = mGuserid;
        replyDao.orderReplyContent = content;
        replyDao.orderId = orderId;
        replyDao.orderReplyIsprivate = 0;
        replyDao.orderReplyRemark = "";
        replyDao.orderReplyTitle = "";
        Gson gson = new Gson();
        String jsonData = gson.toJson(replyDao);
        String url = constant.AllMSG+"/orderReply/modify";
        getCode(url,jsonData);
    }
    private void getCode(final String url,final String jsonData) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String token = SPUtils.getString(mActivity,"token","");
                OkHttpClient okHttpClient = new OkHttpClient();
                MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                RequestBody body = RequestBody.create(JSON, jsonData);
                Request request = new Request.Builder().addHeader("Accept","application/json")
                        .addHeader("Connection","keep-alive").addHeader("Authorization", token)
                        .url(url)
                        .post(body)
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        System.out.println(e);
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String request = response.body().string();
                        System.out.println(request);
                        try {
                            JSONObject codeObj = new JSONObject(request);
                            if(codeObj.getInt("code")==0)
                            {

                            }
                            else
                            {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }).start();
    }
}
